package com.sy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.EquipStateDto;
import com.sy.dto.EquipmentDto;
import com.sy.dto.JsonResult;
import com.sy.entity.Equipment;
import com.sy.exception.BusinessException;
import com.sy.service.EquipmentService;
import com.sy.vo.EquipmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author easonsy
 * @Description
 * @createTime 2022/06/25 16:00:00
 */
@RestController
@RequestMapping("equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping("show")
    public EquipmentDto showAllEquipments(HttpSession session, EquipmentVo equipmentVo) {
        if (session.getAttribute("loginAdminUser")==null){
            throw new BusinessException("管理员未登录");
        }
        PageHelper.startPage(Integer.parseInt(equipmentVo.getCurrentPage()),Integer.parseInt(equipmentVo.getPageSize()));
        Page<Equipment> equipment = (Page<Equipment>) equipmentService.showAllEquipment();
        EquipmentDto equipmentDto = new EquipmentDto();
        equipmentDto.setEquipmentList(equipment);
        equipmentDto.setTotal(equipment.getTotal());
        return equipmentDto;
    }

    @RequestMapping("searchEquipByName")
    public EquipmentDto searchEquipByName(HttpSession session,EquipmentVo equipmentVo) {
        if (session.getAttribute("loginAdminUser") == null) {
            return null;
        }
        PageHelper.startPage(Integer.parseInt(equipmentVo.getCurrentPage()),Integer.parseInt(equipmentVo.getPageSize()));
        Page<Equipment> equipments =(Page<Equipment>) equipmentService.findAllEquipsByName(equipmentVo);
        EquipmentDto equipmentDto = new EquipmentDto();
        equipmentDto.setEquipmentList(equipments);
        equipmentDto.setTotal(equipments.getTotal());
        return equipmentDto;
    }

    @RequestMapping("showState")
    public List<EquipStateDto> showEquipStates(HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException("管理员未登录");
        }

        return equipmentService.showEquipState();
    }

    @RequestMapping("alterState")
    public JsonResult alterState(Integer id, Integer stateId, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            return new JsonResult().setData(404).setErrMsg("管理员未登录");
        }
        if (equipmentService.alterEquipmentState(id, stateId)) {
            return new JsonResult().setCode(200).setData("修改状态成功");
        } else {
            return new JsonResult().setCode(404).setData("修改失败");
        }
    }

    @RequestMapping("add")
    public JsonResult addEquipment(EquipmentVo equipmentVo, HttpSession session) {
        if (session.getAttribute("loginAdminUser")==null){
            return new JsonResult().setData(404).setErrMsg("管理员未登录");
        }
        if (equipmentService.addEquipment(equipmentVo)) {
            return new JsonResult().setCode(200).setData("添加成功");
        } else {
            return new JsonResult().setCode(404).setData("添加失败");
        }
    }

    @RequestMapping("del")
    public JsonResult delEquipment(String id, HttpSession session) {
        if (session.getAttribute("loginAdminUser")==null){
            return new JsonResult().setData(404).setErrMsg("管理员未登录");
        }
        if (equipmentService.delEquipment(Integer.valueOf(id))) {
            System.out.println("进入删除");
            return new JsonResult().setCode(201).setData("删除成功");
        } else {
            return new JsonResult().setCode(404).setData("删除失败");
        }
    }
}
